package team.knowers.qzhserver.user.user.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import team.knowers.qzhserver.common.api.CommonResult;
import team.knowers.qzhserver.user.user.entity.dao.UserDO;
import team.knowers.qzhserver.user.user.entity.dto.UserUpdateDTO;
import team.knowers.qzhserver.user.user.service.UserService;
import team.knowers.qzhserver.user.user.service.impl.UserServiceImpl;
import team.knowers.qzhserver.user.userData.entity.vo.UserDataDetailsVO;
import team.knowers.qzhserver.user.userData.service.UserDataService;
import team.knowers.qzhserver.user.userData.service.impl.UserDataServiceImpl;

import java.security.Principal;

/**
 * @author xtg
 * @date 2024/2/14 21:51
 */
@Tag(name = "UserController", description = "用户配置信息")
@RequestMapping("/user")
@Controller
public class UserController {
    @Autowired
    private UserServiceImpl userService;
    private UserDataServiceImpl userDataService;
    @Operation(summary = "根据id获取指定用户配置信息")
    @GetMapping("/{id}")
    @ResponseBody
    public CommonResult<UserDO> getItem(@PathVariable String id) {
        //防止参数不合理
        if(userService.userStatus(id)==-1)
            return CommonResult.failed("用户id不存在");
        UserDO user = userService.getById(id);
        return CommonResult.success(user);
    }

    @Operation(summary = "删除指定id用户信息")
    @PostMapping(value = "/delete/{id}")
    @ResponseBody
    public CommonResult delete(@PathVariable String id) {
        //防止参数不合理
        if(userService.userStatus(id)==-1)
            return CommonResult.failed("用户id不存在");
        Boolean status = userService.removeById(id);
        if (status) {
            return CommonResult.success("null", "删除成功");
        }
        return CommonResult.failed("删除失败");
    }

    @Operation(summary = "封禁用户")
    @PostMapping("/updateStatusUnable/{id}")
    @ResponseBody
    public CommonResult updateStatusUnable(@PathVariable String id) {
        //防止参数不合理
        if(userService.userStatus(id)==-1)
            return CommonResult.failed("用户id不存在");
        int result = userService.setStatusUnable(id);
        if (result == 1)
            return CommonResult.success(null, "已封禁该用户");
        else
            return CommonResult.failed("该用户不存在");
    }

    @Operation(summary = "解封用户")
    @PostMapping("/updateStatusAble/{id}")
    @ResponseBody
    public CommonResult updateStatusAble(@PathVariable String id) {
        //防止参数不合理
        if(userService.userStatus(id)==-1)
            return CommonResult.failed("用户id不存在");
        int result = userService.setStatusAble(id);
        if (result == 1)
            return CommonResult.success(null, "已解封该用户");
        else
            return CommonResult.failed("该用户不存在");
    }

    @Operation(summary = "获取用户可编辑信息")
    @GetMapping("/getUserEditable")
    @ResponseBody
    public CommonResult<UserUpdateDTO> getUserEditable(@RequestParam String id){
        //防止参数不合理
        if(userService.userStatus(id)==-1)
            return CommonResult.failed("用户id不存在");
        UserUpdateDTO userUpdateDTO = userService.getUserUpdateDTOById(id);
        if (userUpdateDTO!=null)
            return CommonResult.success(userUpdateDTO);
        else
            return CommonResult.failed();
    }

    @Operation(summary = "更新用户可编辑信息")
    @PutMapping("/editUserEditable")
    @ResponseBody
    public CommonResult editUserEditable (@RequestParam String id,@RequestBody UserUpdateDTO userUpdateDTO){
        //防止参数不合理
        if(userService.userStatus(id)==-1)
            return CommonResult.failed("用户id不存在");
        boolean result = userService.editUserEditable(id,userUpdateDTO);
        if(result)
            return CommonResult.success(null,"更新用户信息成功");
        else
            return CommonResult.failed("操作失败，用户不存在");
    }

}
